package priv.blackbibo.application.service;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import priv.blackbibo.application.BootstrapActivity;

/**
 * Created by black on 2019-12-09.
 */
public class VersionService extends Service {
    private static final String TAG = "VersionService";

    public VersionService(){
        Log.d(TAG, "VersionService: init");
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            Thread thread = new Thread(){
                @Override
                public void run() {
                    try {
                        getVersion();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
        }catch (Exception e){
            e.printStackTrace();
        }
        return START_NOT_STICKY;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public void getVersion() throws IOException, PackageManager.NameNotFoundException {
        Log.d(TAG, "getVersion: check version");
        String url = "http://api.fir.im/apps/latest/5dee785b23389f6e67681285?api_token=561496e7cd2fed72c6b137fa4234cd1b";

        OkHttpClient client =new  OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).get().build();
        Response response = client.newCall(request).execute();

        if (response.code() == 200) {
            String res = response.body().string();
            Log.d(TAG, "getVersion: "+res);
            JSONObject result = JSONObject.parseObject(res);
            String version = result.getString("version");
            String versionShort = result.getString("versionShort");
            final String download = result.getString("update_url");
            PackageManager packageManager = getApplicationContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getApplicationContext().getPackageName(), 0);
            long versioncode;
            if (Build.VERSION_CODES.M < Build.VERSION.SDK_INT) {
                versioncode = packageInfo.getLongVersionCode();
            }else {
                versioncode = packageInfo.versionCode;

            }
            if (versioncode != Long.valueOf(version)){
                BootstrapActivity.callShowVersionChanged(download,versionShort);
            }else {
                BootstrapActivity.stepIntoMain();
                Log.d(TAG, "getVersion: it is the latest");
            }
        }
    }
}
